package com.xigege.netty.nettyComponent.futureAndPromise;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

/**
 * 测试Netty中加强的Future
 */
public class TestNettyFuture {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        EventLoopGroup eventExecutors = new NioEventLoopGroup(2);

        Future<String> future = eventExecutors.next().submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("计算结果ing...");
                return "hehe";
            }
        });
        // 同步获取结果
//        System.out.println("等待结果....");
//        System.out.println("结果为："+future.get());

        // 异步获取结果
        future.addListener(new GenericFutureListener<Future<? super String>>(){
            @Override
            public void operationComplete(Future<? super String> future) throws Exception {
                System.out.println("结果为:" + future.getNow());
            }
        });
    }

}
